package com.campus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.common.config.CodeConfig;
import com.campus.entity.*;
import com.campus.entity.Clazz.Clazz;
import com.campus.entity.Clazz.ClazzStudent;
import com.campus.entity.Clazz.ClazzTeacher;
import com.campus.entity.vo.Clazz.ClazzStudentVO;
import com.campus.entity.vo.Clazz.ClazzTeacherVO;
import com.campus.entity.vo.Clazz.ClazzVO;
import com.campus.entity.vo.ClazzUserVO;
import com.campus.entity.vo.Teacher.TeacherVO2;
import com.campus.service.*;
import com.campus.util.CopyPropertiesUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: AlaskaBoo
 * @Date: 2022-10-21 12:56
 **/
@RestController
@Transactional
@RequestMapping("/api/clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private ClazzTeacherService clazzTeacherService;
    @Autowired
    private ClazzStudentService clazzStudentService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 查询班级下的学生
     *
     * @param clazzNum
     * @return
     */
    @GetMapping("/queryClazzStudent/clazzNum={clazzNum}")
    @Cacheable(value = "clazz")
    public ResultBody getClazzStudent(@PathVariable("clazzNum") Integer clazzNum) {
        Clazz clazz = clazzService.lambdaQuery().eq(Clazz::getClassNum,clazzNum).one();
        if (clazz == null) {
            return ResultBody.error().message(CodeConfig.QUERY_FAIL.message);
        }
        List<Student> studentList = studentService.list(new QueryWrapper<Student>().inSql("id", "select student_id from t_clazz_student where class_id=" + clazz.getId()));
        List<ClazzUserVO> collect = studentList.stream().map(student -> CopyPropertiesUtils.copyProperties(student, ClazzUserVO.class)).collect(Collectors.toList());
        ClazzStudentVO clazzStudentVO = new ClazzStudentVO();
        BeanUtils.copyProperties(clazz,clazzStudentVO);
        clazzStudentVO.setStudentList(collect);
        return ResultBody.ok().data("clazzStudentList",clazzStudentVO).message(CodeConfig.QUERY_SUCCESS.message);
    }
    /**
     * 查询班级下的老师
     *
     * @param clazzNum
     * @return
     */
    @GetMapping("/queryClazzTeacher/clazzNum={clazzNum}")
    @Cacheable(value = "clazz")
    public ResultBody getClazzTeacher(@PathVariable("clazzNum") Integer clazzNum) {
        Clazz clazz = clazzService.lambdaQuery().eq(Clazz::getClassNum,clazzNum).one();
        if (clazz == null) {
            return ResultBody.error().message(CodeConfig.QUERY_FAIL.message);
        }
        List<Teacher> teacherList = teacherService.list(new QueryWrapper<Teacher>().inSql("id", "select teacher_id from t_clazz_teacher where class_id="+ clazz.getId()));
        List<ClazzUserVO> collect = teacherList.stream().map(teacher -> CopyPropertiesUtils.copyProperties(teacher, ClazzUserVO.class)).collect(Collectors.toList());
        ClazzTeacherVO clazzTeacherVO = new ClazzTeacherVO();
        BeanUtils.copyProperties(clazz,clazzTeacherVO);
        clazzTeacherVO.setTeacherList(collect);
        return ResultBody.ok().data("clazzTeacherList",clazzTeacherVO).message(CodeConfig.QUERY_SUCCESS.message);
    }
    /**
     * 查询老师下的班级
     *
     * @param account
     * @return
     */
    @GetMapping("/queryTeacherClazz/teacherAccount={account}")
    @Cacheable(value = "clazz")
    public ResultBody getTeacherClazz(@PathVariable("account") String account) {
        Teacher teacher1 = teacherService.lambdaQuery().eq(Teacher::getAccount, account).one();
        if (teacher1 == null) {
            return ResultBody.error().message(CodeConfig.QUERY_FAIL.message);
        }
        List<Clazz> clazzList = clazzService.list(new QueryWrapper<Clazz>().inSql("id", "select class_id from t_clazz_teacher where teacher_id="+ teacher1.getId()));
        List<ClazzVO> collect = clazzList.stream().map(clazz -> CopyPropertiesUtils.copyProperties(clazz, ClazzVO.class)).collect(Collectors.toList());
        TeacherVO2 teacherVO = new TeacherVO2();
        BeanUtils.copyProperties(teacher1,teacherVO);
        teacherVO.setClazzList(collect);
        return ResultBody.ok().data("teacherClazzList",teacherVO).message(CodeConfig.QUERY_SUCCESS.message);
    }
    /**
     * 模糊查询
     *
     * @param currentPage
     * @param pageSize
     * @param clazz
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    @Cacheable(value = "clazz")
    public ResultBody getClazz(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, Clazz clazz) {
        //分页信息封装Page对象
        Page<Clazz> page = new Page<>(currentPage, pageSize);
        //进行查询
        IPage<Clazz> clazzInfo = clazzService.getClazzPage(page, clazz);
        List<Clazz> clazzList = clazzInfo.getRecords();
        List<ClazzVO> collect = clazzList.stream().map(clazz1 -> CopyPropertiesUtils.copyProperties(clazz1, ClazzVO.class)).collect(Collectors.toList());
        if (clazzInfo.getTotal() > 0) {
            //封装Result返回
            return ResultBody.ok().data("clazzInfo", collect).message(CodeConfig.QUERY_SUCCESS.message);
        }
        return ResultBody.error().message(CodeConfig.QUERY_FAIL.message);
    }

    /**
     * 更新班级学生信息
     *
     * @param id
     * @param studentIds
     * @return
     */
    @CacheEvict(value = "clazz", allEntries =true)
    @PostMapping("/updateClazzStudentIds/{id}")
    public ResultBody updateStudents(@PathVariable(value = "id") Long id, @RequestBody Long[] studentIds) {
        List<ClazzStudent> clazzStudentList = new ArrayList<>();
        Arrays.stream(studentIds).forEach(studentId -> {
            ClazzStudent clazzStudent = new ClazzStudent();
            clazzStudent.setStudentId(studentId);
            clazzStudent.setClassId(id);
            clazzStudentList.add(clazzStudent);
        });
        clazzStudentService.remove(new QueryWrapper<ClazzStudent>().eq("class_id", id));
        clazzStudentService.saveBatch(clazzStudentList);
        return ResultBody.ok().data("clazzStudentList", clazzStudentList);
    }

    /**
     * 更新班级老师信息
     *
     * @param id
     * @param teacherIds
     * @return
     */
    @PostMapping("/updateClazzTeacherIds/{id}")
    @CacheEvict(value = "clazz", allEntries =true)
    public ResultBody updateTeachers(@PathVariable(value = "id") Long id, @RequestBody Long[] teacherIds) {
        List<ClazzTeacher> clazzTeacherList = new ArrayList<>();
        Arrays.stream(teacherIds).forEach(teacherId -> {
            ClazzTeacher clazzTeacher = new ClazzTeacher();
            clazzTeacher.setTeacherId(teacherId);
            clazzTeacher.setClassId(id);
            clazzTeacherList.add(clazzTeacher);
        });
        clazzTeacherService.remove(new QueryWrapper<ClazzTeacher>().eq("class_id", id));
        clazzTeacherService.saveBatch(clazzTeacherList);
        return ResultBody.ok().data("clazzTeacherList", clazzTeacherList);
    }
}
